﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VsSpeakerCompanion;
using System.ComponentModel.Composition;
using System.IO;

namespace SoundSnippetWidget
{
    /// <summary>
    /// Allows the speaker to play three different sounds at any moment by clicking on any of the three provided buttons
    /// </summary>
    public class Widget : IWidget
    {
        // our UI
        SoundSnippet soundSnippet;
        // core context to access Demo Dashboard settings
        Context context;

        public Widget(Context context)
        {
            this.context = context;

            soundSnippet = new SoundSnippet();
            soundSnippet.DataContext = this;

            // handlers for our three buttons
            soundSnippet.PlaySound1Clicked += new EventHandler(soundSnippet_PlaySound1Clicked);
            soundSnippet.PlaySound2Clicked += new EventHandler(soundSnippet_PlaySound2Clicked);
            soundSnippet.PlaySound3Clicked += new EventHandler(soundSnippet_PlaySound3Clicked);
        }

        public string Sound1Description
        {
            get { return GetSoundDescription("Sound1"); }
        }

        public string Sound2Description
        {
            get { return GetSoundDescription("Sound2"); }
        }

        public string Sound3Description
        {
            get { return GetSoundDescription("Sound3"); }
        }

        private string GetSoundDescription(string settingSoundKey)
        {
            string soundFile = GetSoundFullPath(context.Settings.Get<string>(settingSoundKey));
            return string.IsNullOrEmpty(soundFile) ? "default" : Path.GetFileNameWithoutExtension(soundFile);
        }

        private void soundSnippet_PlaySound1Clicked(object source, EventArgs e)
        {
            PlaySound(context.Settings.Get<string>("Sound1"));
        }

        private void soundSnippet_PlaySound2Clicked(object source, EventArgs e)
        {
            PlaySound(context.Settings.Get<string>("Sound2"));
        }

        private void soundSnippet_PlaySound3Clicked(object source, EventArgs e)
        {
            PlaySound(context.Settings.Get<string>("Sound3"));
        }

        private void PlaySound(string settingSoundKey)
        {
            try
            {
                string configuredSoundPath = GetSoundFullPath(settingSoundKey);

                // check the sound file exists
                if (File.Exists(configuredSoundPath))
                {
                    System.Media.SoundPlayer sound = new System.Media.SoundPlayer(configuredSoundPath);
                    sound.Play();
                }
                else
                {
                    // if the specified sound file doesn't exists
                    // play the default (embedded) sound
                    // If you want to change the default sound look into the Resources folder
                    using (Stream defaultSound = SoundSnippetWidget.Resources.SoundFile.DefaultSound)
                    {
                        System.Media.SoundPlayer sound = new System.Media.SoundPlayer(defaultSound);
                        sound.Play();
                    }
                }
            }
            catch
            { }
        }

        private string GetSoundFullPath(string sound)
        {
            try
            {
                if (!string.IsNullOrEmpty(sound))
                {
                    if (File.Exists(sound))
                    {
                        return sound;
                    }
                    else if (!Path.IsPathRooted(sound))
                    {
                        string soundInTheSettingsPath = Path.Combine(Path.GetDirectoryName(this.context.Settings.SettingsPath), sound);

                        if (File.Exists(soundInTheSettingsPath))
                        {
                            return soundInTheSettingsPath;
                        }
                    }
                }
            }
            catch
            {
                return sound;
            }

            return sound;
        }

        public System.Windows.UIElement VisualElement
        {
            get
            {
                // this is our UI
                return soundSnippet;
            }
        }

        public void Reset()
        {
            // this widget doesn't use any data that needs to be reset
        }
    }
}
